#include "librarydock.h"
#include "libraryfilter.h"
#include "librarytree.h"
#include "librarymodel.h"
#include "../qtcreator/filterlineedit.h" // Part of Qt Creator

#include <QtCore/qtimer.h>
#include <QtGui/qboxlayout.h>
#include <QtGui/qcombobox.h>
#include <QtGui/qlabel.h>
#include <QtGui/qstackedwidget.h>
#include <QtGui/qtoolbutton.h>
#include <QtGui/qtreewidget.h>

LibraryDock::LibraryDock(LibraryModel *libModel, QWidget *parent) :
    QDockWidget(tr("Library"), parent)
{
    m_libModel = libModel;
    setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    m_trees = new QStackedWidget(this);
    m_trees->addWidget(new LibraryTree(LibraryTree::LibraryMode, libModel, m_trees));
    m_trees->addWidget(new LibraryTree(LibraryTree::GroupMode, libModel, m_trees));
    m_trees->addWidget(new LibraryTree(LibraryTree::ReferenceMode, libModel, m_trees));
    m_trees->setCurrentIndex(0);
    for (int i = 0; i < m_trees->count(); ++i)
        connect(qobject_cast<LibraryTree*>(m_trees->widget(i)), SIGNAL(componentEditRequested(QedaComponent*)),
                this, SIGNAL(componentEditRequested(QedaComponent*)));

    QToolButton *expandButton = new QToolButton(this);
    expandButton->setAutoRaise(true);
    expandButton->setIcon(QIcon(":/images/expand.png"));
    expandButton->setIconSize(QSize(20, 20));
    expandButton->setToolTip(tr("Expand all"));
    connect(expandButton, SIGNAL(clicked()), this, SLOT(expandAll()));

    QToolButton *collapseButton = new QToolButton(this);
    collapseButton->setAutoRaise(true);
    collapseButton->setIcon(QIcon(":/images/collapse.png"));
    collapseButton->setIconSize(QSize(20, 20));
    collapseButton->setToolTip(tr("Collapse all"));
    connect(collapseButton, SIGNAL(clicked()), this, SLOT(collapseAll()));

    m_arrangeBox = new QComboBox(this);
    m_arrangeBox->addItem(tr("Library"));
    m_arrangeBox->addItem(tr("Group"));
    m_arrangeBox->addItem(tr("Reference"));
    connect(m_arrangeBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(changeArrangeMode(int)));

    QHBoxLayout *toolsLayout = new QHBoxLayout;
    toolsLayout->setSpacing(2);
    toolsLayout->addWidget(expandButton);
    toolsLayout->addWidget(collapseButton);
    toolsLayout->addWidget(new QLabel(tr("Arrange by:")));
    toolsLayout->addWidget(m_arrangeBox, 1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setSpacing(2);
    mainLayout->setMargin(3);
    mainLayout->addLayout(toolsLayout);
    mainLayout->addWidget(m_trees, 1);

    QWidget *w = new QWidget(this);
    w->setLayout(mainLayout);
    setWidget(w);

    setTitleBarWidget(createFilter());
}

void LibraryDock::changeArrangeMode(int mode)
{
    //m_libModel->setArrangeMode(mode);
    m_trees->setCurrentIndex(mode);
    m_filters->setCurrentIndex(mode);
    //initGroupsBox(mode);
}

void LibraryDock::collapseAll()
{
    for (int i = 0; i < m_trees->count(); ++i)
    {
        QTreeWidget *tree = qobject_cast<QTreeWidget*>(m_trees->widget(i));
        if (tree)
            tree->collapseAll();
    }
}

QWidget *LibraryDock::createFilter()
{
    QWidget *w = new QWidget(this);
    QHBoxLayout *filterLayout = new QHBoxLayout;
    filterLayout->addWidget(new QLabel(tr("Filter:")));

    m_filters = new QStackedWidget(this);
    m_filters->addWidget(new LibraryFilter(LibraryFilter::LibraryMode));
    m_filters->addWidget(new LibraryFilter(LibraryFilter::GroupMode));
    m_filters->addWidget(new LibraryFilter(LibraryFilter::ReferenceMode));
    m_filters->setCurrentIndex(0);

    for (int i = 0; i < m_filters->count(); ++i)
    {
        connect(qobject_cast<LibraryFilter*>(m_filters->widget(i)), SIGNAL(currentIndexChanged(int)),
                qobject_cast<LibraryTree*>(m_trees->widget(i)), SLOT(filterByCategory(int)));
        connect(qobject_cast<LibraryTree*>(m_trees->widget(i)), SIGNAL(categoryAdded(QString)),
                qobject_cast<LibraryFilter*>(m_filters->widget(i)), SLOT(addCategory(QString)));
        connect(qobject_cast<LibraryTree*>(m_trees->widget(i)), SIGNAL(categoryRemoved(QString)),
                qobject_cast<LibraryFilter*>(m_filters->widget(i)), SLOT(removeCategory(QString)));
        //connect(qobject_cast<LibraryFilter*>(m_groups->widget(i)), SIGNAL(currentIndexChanged(int)),
        //        qobject_cast<LibraryTree*>(m_trees->widget(i)), SLOT(filterByGroup(int)));
    }

    filterLayout->addWidget(m_filters, 1);
    m_regExp = new Utils::FilterLineEdit(w);
    filterLayout->addWidget(m_regExp, 1);
    connect(m_regExp, SIGNAL(textChanged(QString)), this, SLOT(filterByRegExp(QString)));
    w->setLayout(filterLayout);
    return w;
}

void LibraryDock::expandAll()
{
    for (int i = 0; i < m_trees->count(); ++i)
    {
        QTreeWidget *tree = qobject_cast<QTreeWidget*>(m_trees->widget(i));
        if (tree)
            tree->expandAll();
    }
}

void LibraryDock::restoreMinMaxSizes()
{
    setMinimumWidth(m_oldMinWidth);
    setMaximumWidth(m_oldMaxWidth);
}

void LibraryDock::setWidth(int w)
{
    m_oldMinWidth = minimumWidth();
    m_oldMaxWidth = maximumWidth();
    if (w > 0)
    {
        if (width()< w)
            setMinimumWidth(w);
        else
            setMaximumWidth(w);
    }
    QTimer::singleShot(1, this, SLOT(restoreMinMaxSizes()));
}
